package com.manage.service.impl;

import com.manage.dao.BuildingDao;
import com.manage.dao.HousingDao;
import com.manage.dao.RoomDao;
import com.manage.dao.impl.BuildingDaoImpl;
import com.manage.dao.impl.HousingDaoImpl;
import com.manage.dao.impl.RoomDaoImpl;
import com.manage.pojo.Housing;
import com.manage.service.WelcomeService;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WelcomeServiceImpl implements WelcomeService {
    @Override
    public Map<String ,Integer> getWelData() {
        Map<String,Integer> map=new HashMap<>();
        //小区数量
        HousingDao housingDao=new HousingDaoImpl();
        BuildingDao buildingDao=new BuildingDaoImpl();
        RoomDao roomDao=new RoomDaoImpl();
        try {
            int hcount = housingDao.findAll().size();
            map.put("hcount",hcount);
            int bcount = buildingDao.findAll().size();
            map.put("bcount",bcount);
            int rcount = roomDao.findAll().size();
            map.put("rcount",rcount);
            return map;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }

    }

    @Override
    public Map<String, Object> getImgData() {
        HousingDao housingDao=new HousingDaoImpl();
        BuildingDao buildingDao=new BuildingDaoImpl();
        RoomDao roomDao=new RoomDaoImpl();
        try {
            List<Housing> housingDaoAll = housingDao.findAll();

            List<Housing> housings=new ArrayList<>();
            List<String> housingnames=new ArrayList<>();
            for (int i=0;i<4;i++){
                housings.add(housingDaoAll.get(i));
                housingnames.add(housings.get(i).getHname());
            }


            List<Map<String,Object>> mapList=new ArrayList<>();
            for (int i=0;i<4;i++){
                Map<String,Object> map=new HashMap<>();
                map.put("name",housingnames.get(i));
                map.put("type","line");
                int[] a=new int[2];
                int rcount = (int)housings.get(i).getRcount();
                int bcount = (int)housings.get(i).getBcount();
                a[0]=rcount;
                a[1]=bcount;
                map.put("data",a);
                mapList.add(map);
            }


            HashMap<String ,Object> hashMap=new HashMap();
            hashMap.put("data",housingnames);
            hashMap.put("series",mapList);

            return hashMap;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }
    }
}
